//
//  ChooseLocationViewController.swift
//  zhendeBusiness
//
//  Created by chenliang on 2017/3/8.
//  Copyright © 2017年 chenliang. All rights reserved.
//

import UIKit

class ChooseLocationViewController: YYController, BMKLocationServiceDelegate, BMKMapViewDelegate {
    
    var chooseClosure: ((Double, Double) -> Void)!

    var mapView : BMKMapView!
    var locService : BMKLocationService!
    
    var lat: Double?
    var lng: Double?
    
    override func yy_configNaviItem(_ naviItem: UINavigationItem) {
        super.yy_configNaviItem(naviItem)
        
        self.title = "选择位置"
        
        let rightButton = UIBarButtonItem(title: "确认", style: .plain, target: self, action: #selector(confirmLocation))
        naviItem.rightBarButtonItem = rightButton
        
        let leftButton = UIBarButtonItem(title: "取消", style: .plain, target: self, action: #selector(cancelLocation))
        naviItem.leftBarButtonItem = leftButton
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        initMapView()
        locService = BMKLocationService()

        if self.lat != nil {
            var coor = CLLocationCoordinate2D()
            coor.latitude = self.lat!
            coor.longitude = self.lng!
            mapView.centerCoordinate = coor
            
            setCurrentAddress()
        } else {
            startLocation()
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    @objc func confirmLocation() {
        chooseClosure(lat!, lng!)
        yy_pop()
    }
    
    @objc func cancelLocation() {
        yy_pop()
    }
    
    func startLocation() {
        locService.startUserLocationService()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        mapView.viewWillAppear()
        mapView.delegate = self
        
        self.navigationController?.navigationBar.isHidden = false
        locService.delegate = self
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        mapView.viewWillDisappear()
        mapView.delegate = nil
        
        locService.delegate = nil
    }
    
    func didUpdate(_ userLocation: BMKUserLocation!) {
        if userLocation != nil {
            yy_Log("didUpdateUserLocation lat:\(userLocation.location.coordinate.latitude) lon:\(userLocation.location.coordinate.longitude)")
            
            self.lat = userLocation.location.coordinate.latitude
            self.lng = userLocation.location.coordinate.longitude
            locService.stopUserLocationService()
            mapView.centerCoordinate = userLocation.location.coordinate
            
            setCurrentAddress()
        }
    }
    
    func mapView(_ mapView: BMKMapView!, regionDidChangeAnimated animated: Bool) {
        
        self.lat = mapView.centerCoordinate.latitude
        self.lng = mapView.centerCoordinate.longitude
        
        setCurrentAddress()
        
    }
    
    func mapView(_ mapView: BMKMapView!, viewFor annotation: BMKAnnotation!) -> BMKAnnotationView! {
        if annotation is BMKPointAnnotation {
            let newAnnotionView = BMKAnnotationView(annotation: annotation, reuseIdentifier: "myannotation")
            
            newAnnotionView?.image = #imageLiteral(resourceName: "location")
            
            return newAnnotionView
        }
        return nil;
    }
    
    
    func setCurrentAddress() {
        mapView.removeAnnotations(mapView.annotations)
        
        var coor = CLLocationCoordinate2D()
        coor.latitude = self.lat!
        coor.longitude = self.lng!
        
        let annotation = BMKPointAnnotation()
        annotation.coordinate = coor
        annotation.title = ""
        mapView.addAnnotation(annotation)
    }

    func initMapView() {
        mapView = BMKMapView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height))
        mapView.zoomLevel = 17
        
        view.addSubview(mapView)
        mapView.snp.makeConstraints { (make) in
            make.left.equalTo(view)
            make.right.equalTo(view)
            make.bottom.equalTo(view)
            make.top.equalTo(view)
        }
        
        mapView.showsUserLocation = true
    }

}
